﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GmUiInplaceEditFamily.h"
#include <iostream>
#include <map>
#include "GbmpGuid.h"
#include "OwnerPtr.h"
#include "QProcess"
#include "EditFamilySharedContent.h"
#include "IFamilyLoadOptions.h"
#include "GbmpDefines.h"

namespace gcmp
{
    class IDocument;
    class  GM_UI_INPLACE_EDIT_FAMILY_EXPORT EditFamilyCommunicationCache
    {
        CANNOT_COPY_OR_ASSIGN(EditFamilyCommunicationCache)
    public:
        virtual ~EditFamilyCommunicationCache() {}

        static EditFamilyCommunicationCache* Get();

        bool SetEditFamilySharedContent(OwnerPtr<EditFamilySharedContent> opContent);
        const EditFamilySharedContent* GetEditFamilySharedContent();

        bool AddLoadFamilyToProjectSharedContent(const std::wstring& connectionGuid, OwnerPtr<LoadFamilyToProjectSharedContent> opContent);
        const LoadFamilyToProjectSharedContent* GetLoadFamilyToProjectSharedContent(const std::wstring& connectionGuid);
        void ClearLoadFamilyToProjectSharedContent();

    protected:
        EditFamilyCommunicationCache();

    private:
        OwnerPtr<EditFamilySharedContent> m_currentEditFamilyContent;

        std::map<std::wstring, OwnerPtr<LoadFamilyToProjectSharedContent>> m_loadFamilyToProjectInfos;
    };

    typedef std::map<std::wstring, OwnerPtr<LoadFamilyToProjectSharedContent>> ProjectDocData;
    class  GM_UI_INPLACE_EDIT_FAMILY_EXPORT ProjectDocumentDataManager
    {
        CANNOT_COPY_OR_ASSIGN(ProjectDocumentDataManager)
    public:
        virtual ~ProjectDocumentDataManager() {}

        static ProjectDocumentDataManager* Get();

        bool AddLoadFamilyToProjectSharedContent(const std::wstring& connectionGuid, OwnerPtr<LoadFamilyToProjectSharedContent> opContent);
        const LoadFamilyToProjectSharedContent* GetLoadFamilyToProjectSharedContent(const std::wstring& connectionGuid);
        const ProjectDocData& GetAllProjectData() { return m_loadFamilyToProjectInfos; }
        void ClearLoadFamilyToProjectSharedContent();

        bool AddWaitedProjectAppGuids(const std::wstring& strAppGuid);
        void ClearWaitedProjectAppGuids();

        bool IsAllProjectDocmentDataUpdated();

    protected:
        ProjectDocumentDataManager();

    private:
        std::map<std::wstring, OwnerPtr<LoadFamilyToProjectSharedContent>> m_loadFamilyToProjectInfos;
        std::set<std::wstring> m_waitProjectAppGuids;
    };

    class  GM_UI_INPLACE_EDIT_FAMILY_EXPORT GmEditFamilyUtils
    {
    public:
        static void SendLoadFamilyToProject(const EditFamilySharedContent * pEditFamilySharedContent,bool isCloseLoadFamily=true);
        static bool GetFamilyLoadStrategy(IDocument* pDoc, const std::wstring& filePathName, FamilyFoundStrategy* pStrategy);
        static bool RequestProjectDocumentData(bool isCloseLoadFamily = true);
        static void OpenFamilyDocument(OwnerPtr<EditFamilySharedContent> opEditFamilyContent);
        static void OpenFamilyDocumentByTemporaryCopy(OwnerPtr<EditFamilySharedContent> opEditFamilyContent);
        static void NewFamilyDocument(OwnerPtr<EditFamilySharedContent> opEditFamilyContent);
        static void NewDoorOrWindowFamilyDocument(OwnerPtr<EditFamilySharedContent> opEditFamilyContent);

        static bool SelectProjectDocAndPostLoadFamily(IDocument* pFamilyDoc, bool isCloseLoadFamily = true);
        static std::wstring GetProjectAppName();
        static void LoadFamilyToMainDoc(const EditFamilySharedContent * pContent);
    };

}